function expfitch()
% test exp fit using chebyshev approximation 
%
x=0:0.001:.199;
noise_amp = 0.0;
noise = noise_amp*(rand(size(x))-0.5);
signal = 2*exp(-x/0.03);
y = signal+noise;
clf;
plot(x,y);
hold on;
% generate chebyshev coeffs of data
cdata = chebft(min(x), max(x), 100, x, y);
cdata
return;







for k=3:12
   p = polyfit(x,y,k);
   f = polyval(p, x);
 %  plot(x,f, 'rx');
   err = max((y-f));
   a = p(k+1);
   t = allt(a, p, k);
   t1 =  -a/(p(k));
   t2 = sqrt(a/(p(k-1)*2));
   t3 = (-a/p(k-2)*3*2)^(1/3);
   disp(sprintf('k: %d, err: %12.5f  a: %8.4f  t1: %8.4f  t2: %8.4f  mean: %8.4f', k, err, a, t(1), t(2), mean(t)))
 %  disp(sprintf('k: %d, err: %12.5f  a: %8.4f  t1: %8.4f  t2: %8.4f  ', k, err, a, t1, t2))
end


function t = allt(a, p, k)
% compute in general:
% t = (a/(p(n)*n!)^(1/n)
% for n = 1 to k-1
% this extracts t from the terms for the taylor series expansion around 0 for ae^(-x/t) 
for n = 1:(k-1)
   odd = rem(n, 2);
   if(odd==1)
      sign=-1;
   else
      sign=1;
   end
   t(n) = (sign*a / (p(k-n+1)*fact(n)))^(1/n);
end

function r=fact(k)
r=1;
for i = 1:k
   r=r*i;
end

   